<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\AdminRole;
use app\admin\model\Admin as Admin;

class Role extends AdminBase{

    //当前控制器菜单
 	public $submenu = [];

 	/**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '角色管理', 'url' => url('admin/role/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '角色添加', 'url' => url('admin/role/add'), 'icon' => '&#xe654;'];
        $this->submenu[] = ['title' => '更新角色缓存', 'url' => url('admin/role/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 角色管理
     * @access public
     * @return \think\Response
     */
    public function index() {
    	$this->assign([
            'list' => AdminRole::getRoleAll(),
            'submenu' => $this->submenu,
        ]);
    	return $this->fetch();
    }

    /**
     * 角色权限
     * @access public
     * @return \think\Response
     */
    public function auth($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少角色ID');
        }
        $admin_role = AdminRole::get($id);
        if((int)$admin_role->id == 1){
            $this->error('['.$admin_role->name.']拥有最高权限，无需设置');
        }
        // 提交更新内容
        if($this->request->isPost()){
            $data = $this->request->param('data/a');
            $admin_role->system = $data;
            if(false !== $admin_role->save()){
               $admin_role->cache();
               $this->success('角色权限修改成功');
            }else{
                $this->error($admin_role->getError());
            }
        }
        $this->assign([
            'system_auth' => $admin_role->system,
            'list' => config('manniu_auth'),
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 角色添加
     * @access public
     * @return \think\Response
     */
    public function add() {
        $admin_role_model = new AdminRole;
        if($this->request->isPost()){
            $post_data = $this->request->param('data/a');
            if($admin_role_model->where('name',$post_data['name'])->count()){
                $this->error('角色组名称已存在');
            }
            if($admin_role_model->allowField(true)->validate(true)->save($post_data)){
                $admin_role_model->cache();
                $this->success('角色添加成功','index');
            }else{
                $this->error($admin_role_model->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 角色修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0) {
        $id = (int)$id;
        if(!$id){
            $this->error('角色参数错误');
        }

        $data = AdminRole::get($id);
        if(!$data){
            $this->error('角色不存在');
        }
        // 表单提交操作
        if($this->request->isPost()){
            $post_data = $this->request->param('data/a');
            if((int)$data->id == 1 && (int)$post_data['status'] == 0){
                $this->error('['.$data->name.']角色不能禁用');
            }
            if($data->where('id','<>',(int)$data->id)->where('name',$post_data['name'])->count()){
                $this->error('角色组名称已存在');
            }
            if(false !== $data->allowField(true)->validate(true)->save($post_data,['id'=>(int)$data->id])){
                $data->cache();
                $this->success('角色修改成功','index');
            }else{
                $this->error($data->getError());
            }
        }
        // 向模版赋值
        $this->assign([
            'data' => $data,
            'submenu' => $this->submenu,
        ]);
        return $this->fetch();
    }

    /**
     * 角色删除
     * @access public
     * @return void
     */
    public function del($id = 0) {
        $id = (int)$id;
        if(!$id){
            $this->error('角色参数错误');
        }

        if($id == 1 || $id == 2){
            $this->error('系统角色不允许删除');
        }
        $data = AdminRole::get($id);
        if(!$data){
            $this->error('角色不存在');
        }

        //判断此角色是否存在管理员
        if(Admin::get(['role_id' => $data->id])){
            $this->error('此角色下存在管理员，不能删除');
        }

        $data->delete();
        $data->cache();
        $this->success('角色删除成功','index');
    }

    /**
     * 更新角色缓存
     * @access public
     * @return void
     */
    public function cache() {
        $admin_role_model = new AdminRole;
    	$admin_role_model->cache();
    	$this->success('更新角色缓存成功','index');
    }
}